home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / INTERP.C < prev    next >
C/C++ Source or Header  |  1992-02-05  |  24KB  |  762 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* interp.c */
  21. /* Ghostscript language interpreter */
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "errors.h"
  25. #include "estack.h"
  26. #include "name.h"
  27. #include "dict.h"
  28. #include "oper.h"
  29. #include "packed.h"
  30. #include "save.h"
  31. #include "stream.h"
  32. #include "store.h"
  33.  
  34. /* Imported operator procedures */
  35. extern int obj_le(P2(os_ptr, os_ptr));
  36. extern int zop_add(P1(os_ptr));
  37. extern int zop_sub(P1(os_ptr));
  38.  
  39. /* The procedure to call if an operator requests rescheduling. */
  40. /* This causes an error unless the context machinery has been installed. */
  41. private int no_reschedule(P0());
  42. int (*gs_interp_reschedule_proc)(P0()) = no_reschedule;
  43. /* The procedure to call for time-slicing. */
  44. /* This is a no-op unless the context machinery has been installed. */
  45. private int no_time_slice(P0());
  46. int (*gs_interp_time_slice_proc)(P0()) = no_time_slice;
  47.  
  48. /* Forward references */
  49. private int interp(P1(ref *pref));
  50. private int interp_exit(P1(os_ptr));
  51. private int i_interp_exit;
  52. private int copy_stack(P3(ref *, uint, ref *));
  53.  
  54. /* Configuration parameters */
  55. #define max_ostack 800
  56. #define max_estack 150
  57. #define max_dstack 20
  58. #define num_clear_dstack 2
  59.  
  60. /* See estack.h for a description of the execution stack. */
  61.  
  62. /* The logic for managing icount and iref below assumes that */
  63. /* there are no control operators which pop and then push */
  64. /* information on the execution stack. */
  65.  
  66. /* Stacks */
  67. #define os_guard_under 10
  68. #define os_guard_over 10
  69. private ref ostack[os_guard_under+max_ostack+os_guard_over];
  70. private ref estack[max_estack];
  71. ref dstack[max_dstack];
  72. os_ptr osp_nargs[os_max_nargs];        /* for checking osp */
  73.  
  74. /* Stack pointers */
  75. os_ptr osbot, osp, ostop;
  76. es_ptr esbot, esp, estop;
  77. ds_ptr dsp, dstop;
  78.  
  79. /* The object that caused an error */
  80. ref error_object;
  81.  
  82. /* Object related to error handling */
  83. extern ref name_errordict;
  84. extern ref name_ErrorNames;
  85.  
  86. /* Extended types.  The interpreter may replace the type of operators */
  87. /* in procedures with these, to speed up the interpretation loop. */
  88. #define tx_op t_next_index
  89. extern int zadd(P1(os_ptr));
  90. extern int zdup(P1(os_ptr));
  91. extern int zexch(P1(os_ptr));
  92. extern int zif(P1(os_ptr));
  93. extern int zifelse(P1(os_ptr));
  94. extern int zle(P1(os_ptr));
  95. extern int zpop(P1(os_ptr));
  96. extern int zroll(P1(os_ptr));
  97. extern int zsub(P1(os_ptr));
  98. private op_proc_p special_ops[] = {
  99.     zadd, zdup, zexch, zif, zifelse, zle, zpop, zroll, zsub
  100. };
  101. typedef enum {
  102.     tx_op_add = tx_op,
  103.     tx_op_dup,
  104.     tx_op_exch,
  105.     tx_op_if,
  106.     tx_op_ifelse,
  107.     tx_op_le,
  108.     tx_op_pop,
  109.     tx_op_roll,
  110.     tx_op_sub,
  111.     tx_next_op
  112. } special_op_types;
  113. #define num_special_ops ((int)tx_next_op - tx_op)
  114. #define t_invalid tx_next_op        /* first invalid type */
  115.  
  116. /* The following is a special "error" code that is used internally */
  117. /* to cause the interpreter to exit. */
  118. #define e_InterpreterExit (-100)
  119.  
  120. /* Initialize the interpreter */
  121. void
  122. interp_init(int ndict)
  123. {    /* Initialize the guard entries on the operand stack */
  124.     /* with objects that have invalid type and attributes. */
  125.     osbot = ostack + os_guard_under;
  126.     osp = osbot - 1, ostop = osbot + (max_ostack-1);
  127.        {    register os_ptr op;
  128.         for ( op = ostack; op < osbot; op++ )
  129.             make_tav(op, t_invalid, 0, index, 0);
  130.        }
  131.        {    register int i;
  132.         for ( i = 1; i <= os_max_nargs; i++ )
  133.             op_nargs_check(i) = osbot + i - 1;
  134.        }
  135.     esbot = estack, esp = estack - 1, estop = estack + (max_estack-1);
  136.     /* Initialize the dictionary stack to the first ndict */
  137.     /* dictionaries.  ndict is a parameter because during */
  138.     /* initialization, only systemdict exists. */
  139.     dsp = dstack + ndict - 1, dstop = dstack + (max_dstack-1);
  140. }
  141. private int
  142. no_reschedule()
  143. {    return e_invalidcontext;
  144. }
  145. private int
  146. no_time_slice()
  147. {    return 0;
  148. }
  149.  
  150. /* Look up an operator during initialization, */
  151. /* changing its type if appropriate. */
  152. void
  153. interp_fix_op(ref *opref)
  154. {    register int i = num_special_ops;
  155.     op_proc_p proc = real_opproc(opref);
  156.     while ( --i >= 0 && proc != special_ops[i] ) ;
  157.     if ( i >= 0 )
  158.       make_tav(opref, tx_op + i, a_executable, opproc,
  159.            (dummy_op_proc_p)proc);
  160. }
  161.  
  162. /* Invoke the interpreter.  If execution completes normally, return 0. */
  163. /* if an error occurs, then if user_errors is true and the error is a */
  164. /* recoverable one (not an overflow condition), let the user handle it; */
  165. /* otherwise, return the error code. */
  166. int
  167. gs_interpret(ref *pref, int user_errors)
  168. {    ref *epref = pref;
  169.     ref erref;
  170.     ref *perrordict, *pErrorNames;
  171.     int code, ccode;
  172.     ref saref;
  173.     /* Push a special exit procedure on the execution stack */
  174.     es_ptr esp0 = ++esp;
  175.     make_oper(esp0, i_interp_exit, (dummy_op_proc_p)interp_exit);
  176. retry:    code = interp(epref);
  177.     if ( code == e_InterpreterExit ) return 0;
  178.     /* Adjust osp in case of operand stack underflow */
  179.     if ( osp < osbot - 1 )
  180.         osp = osbot - 1;
  181.     if ( !user_errors ) return code;
  182.     if ( dict_find(&systemdict, &name_errordict, &perrordict) <= 0 ||
  183.          dict_find(&systemdict, &name_ErrorNames, &pErrorNames) <= 0
  184.        )
  185.         return code;    /* errordict or ErrorNames not found?? */
  186.     switch ( code )
  187.        {
  188.     case e_dictstackoverflow:
  189.         if ( osp + 1 >= ostop ) return e_stackoverflow;
  190.         ccode = copy_stack(dstack, dsp - dstack + 1, &saref);
  191.         if ( ccode < 0 ) return ccode;
  192.         dsp = &dstack[num_clear_dstack - 1];
  193.         *++osp = saref;
  194.         break;
  195.     case e_execstackoverflow:
  196.         if ( osp + 1 >= ostop ) return e_stackoverflow;
  197.         ccode = copy_stack(estack, esp - estack + 1, &saref);
  198.         if ( ccode < 0 ) return ccode;
  199.         esp = esp0;
  200.         *++osp = saref;
  201.         break;
  202.     case e_stackoverflow:
  203.         ccode = copy_stack(ostack, osp - osbot + 1, &saref);
  204.         if ( ccode < 0 ) return ccode;
  205.         osp = osbot;
  206.         *osbot = saref;
  207.         break;
  208.        }
  209.     if ( -code > r_size(pErrorNames) )
  210.         return code;        /* unknown error??? */
  211.     if ( dict_find(perrordict, &pErrorNames->value.refs[-code - 1], &epref) <= 0 )
  212.         return code;        /* error name not in errordict??? */
  213.     erref = *epref;
  214.     epref = &erref;
  215.     /* Push the error object on the operand stack */
  216.     *++osp = error_object;
  217.     goto retry;
  218. }    
  219. private int
  220. interp_exit(os_ptr op)
  221. {    return e_InterpreterExit;
  222. }
  223.  
  224. /* Copy the contents of an overflowed stack into an array. */
  225. private int
  226. copy_stack(ref *stk, uint size, ref *arr)
  227. {    ref *abody = alloc_refs(size, "overflowed stack");
  228.     if ( abody == 0 ) return e_VMerror;
  229.     refcpy_to_new(abody, stk, size);
  230.     make_tasv(arr, t_array, a_all, size, refs, abody);
  231.     return 0;
  232. }
  233.  
  234. /* Main interpreter. */
  235. /* If execution terminates normally, return e_InterpreterExit. */
  236. /* If an error occurs, leave the current object in error_object */
  237. /* and return a (negative) error code. */
  238. #define return_with_error(code, objp)\
  239.   { esp = iesp; osp = iosp; error_object = *(objp); return_error(code); }
  240. private int
  241. interp(ref *pref /* object to interpret */)
  242. {    register ref *iref = pref;
  243.     register int icount = 0;    /* # of consecutive tokens at iref */
  244.     register os_ptr iosp = osp;    /* private copy of osp */
  245.     register es_ptr iesp = esp;    /* private copy of esp */
  246.     int code;
  247.     ref token;        /* token read from file or string, */
  248.                 /* must be declared in this scope */
  249.     register ref *pvalue;
  250.     os_ptr whichp;
  251.     /* We want to recognize executable arrays here, */
  252.     /* so we push the argument on the estack and enter */
  253.     /* the loop at the bottom. */
  254.     if ( iesp >= estop ) return_with_error (e_execstackoverflow, pref);
  255.     *++iesp = *pref;
  256.     goto bot;
  257.     /* At this point, if icount > 0, iref and icount correspond */
  258.     /* to the top entry on the execution stack: icount is the */
  259.     /* count of sequential entries remaining AFTER the current one. */
  260. #define add1_short(pref) (ref *)((ushort *)(pref) + 1)
  261. #define store_state(ep)\
  262.   ( icount > 0 ? (ep->value.refs = iref + 1, r_set_size(ep, icount)) : 0 )
  263. #define store_state_short(ep)\
  264.   ( icount > 0 ? (ep->value.refs = add1_short(iref), r_set_size(ep, icount)) : 0 )
  265. #define next()\
  266.   if ( --icount > 0 ) { iref++; goto top; } else goto out
  267. #define next_short()\
  268.   if ( --icount <= 0 ) { if ( icount < 0 ) goto up; iesp--; }\
  269.   iref = add1_short(iref); goto top;
  270. top:    /*
  271.      * This is the top of the interpreter loop.
  272.      * iref points to the ref being interpreted.
  273.      * Note that this might be an element of a packed array,
  274.      * not a real ref: we carefully arranged the first 16 bits of
  275.      * a ref and of a packed array element so they could be distinguished
  276.      * from each other.  (See ghost.h and packed.h for more detail.)
  277.      */
  278. #ifdef DEBUG
  279. if ( gs_debug['I'] || gs_debug['i'] &&
  280.      (*(ushort *)iref <= packed_max_full_ref ? r_type(iref) == t_name :
  281.       *(short *)iref < 0)
  282.    )
  283.    {    void debug_print_ref(P1(ref *));
  284.     int edepth = iesp - esbot;
  285.     char depth[10];
  286.     sprintf(depth, "%2d", edepth);
  287.     dputs(depth);
  288.     edepth -= strlen(depth);
  289.     do { dputc('.'); } while ( --edepth > 0 );    /* indent */
  290.     dprintf3("%lx(%2d)<%2d>: ",
  291.          (ulong)iref, icount, (uint)(iosp - osbot + 1));
  292.     debug_print_ref(iref);
  293.     if ( iosp >= osbot )
  294.        {    dputs(" // ");
  295.         debug_print_ref(iosp);
  296.        }
  297.     dputc('\n');
  298.     fflush(dstderr);
  299.    }
  300. #endif
  301. /* Object that have attributes (arrays, dictionaries, files, and strings) */
  302. /* use lit and exec; other objects use plain and plain_exec. */
  303. #define rsa(attrs) (attrs >> (r_type_shift - 2))
  304. #define rsa_execute rsa(a_execute)
  305. #define rsa_executable rsa(a_executable)
  306. #define lit(t) (((t) << 2) + rsa_execute)
  307. #define exec(t) (((t) << 2) + rsa_execute + rsa_executable)
  308. #define nox(t) ((t) << 2)
  309. #define nox_exec(t) (((t) << 2) + rsa_executable)
  310. #define plain(t) ((t) << 2)
  311. #define plain_exec(t) (((t) << 2) + rsa_executable)
  312.     /*
  313.      * We have to populate enough cases of the switch statement to force
  314.      * some compilers to use a dispatch rather than a testing loop.
  315.      * What a nuisance!
  316.      */
  317.     switch ( r_type_xe(iref) )
  318.        {
  319.     /* Access errors. */
  320. #define cases_nox()\
  321.   case nox_exec(t_array): case nox_exec(t_dictionary):\
  322.   case nox_exec(t_file): case nox_exec(t_string):\
  323.   case nox_exec(t_mixedarray): case nox_exec(t_shortarray)
  324.     cases_nox():
  325.         return_with_error (e_invalidaccess, iref);
  326.     /*
  327.      * Literal objects.  We have to enumerate all the types.
  328.      * In fact, we have to include some extra plain_exec entries
  329.      * just to populate the switch.  We break them up into groups
  330.      * to avoid overflowing some preprocessors.
  331.      */
  332. #define cases_lit_1()\
  333.   case lit(t_array): case nox(t_array):\
  334.   case plain(t_boolean): case plain_exec(t_boolean):\
  335.   case plain(t_condition): case plain_exec(t_condition):\
  336.   case lit(t_dictionary): case nox(t_dictionary):\
  337.   case lit(t_file): case nox(t_file):\
  338.   case plain(t_fontID): case plain_exec(t_fontID):\
  339.   case plain(t_gstate): case plain_exec(t_gstate)
  340. #define cases_lit_2()\
  341.   case plain(t_integer): case plain_exec(t_integer):\
  342.   case plain(t_lock): case plain_exec(t_lock):\
  343.   case plain(t_mark): case plain_exec(t_mark):\
  344.   case plain(t_name):\
  345.   case plain(t_null):\
  346.   case plain(t_oparray):\
  347.   case plain(t_operator):\
  348.   case plain(t_real): case plain_exec(t_real)
  349. #define cases_lit_3()\
  350.   case plain(t_save): case plain_exec(t_save):\
  351.   case lit(t_string): case nox(t_string):\
  352.   case lit(t_mixedarray): case nox(t_mixedarray):\
  353.   case lit(t_shortarray): case nox(t_shortarray):\
  354.   case plain(t_color): case plain_exec(t_color):\
  355.   case plain(t_device): case plain_exec(t_device)
  356.     cases_lit_1():
  357.     cases_lit_2():
  358.     cases_lit_3():
  359.         break;
  360.     /* Special operators. */
  361.     case plain_exec(tx_op_add):
  362. x_add:        if ( (code = zop_add(iosp)) < 0 )
  363.             return_with_error (code, iref);
  364.         iosp--;
  365.         next();
  366.     case plain_exec(tx_op_dup):
  367. x_dup:        if ( iosp < op_nargs_check(1) )
  368.             return_with_error (e_stackunderflow, iref);
  369.         iosp++;
  370.         ref_assign(iosp, iosp - 1);
  371.         next();
  372.     case plain_exec(tx_op_exch):
  373. x_exch:        if ( iosp < op_nargs_check(2) )
  374.             return_with_error (e_stackunderflow, iref);
  375.         ref_assign(&token, iosp);
  376.         ref_assign(iosp, iosp - 1);
  377.         ref_assign(iosp - 1, &token);
  378.         next();
  379.     case plain_exec(tx_op_if):
  380. x_if:        if ( !r_has_type(iosp - 1, t_boolean) )
  381.           return_with_error (e_typecheck, iref);
  382.         if ( !iosp[-1].value.index )
  383.           { iosp -= 2;
  384.             next();
  385.           }
  386.         if ( iesp >= estop )
  387.           return_with_error (e_execstackoverflow, iref);
  388.         store_state(iesp);
  389.         whichp = iosp;
  390.         iosp -= 2;
  391.         goto ifup;
  392.     case plain_exec(tx_op_ifelse):
  393. x_ifelse:    if ( !r_has_type(iosp - 2, t_boolean) )
  394.             return_with_error (e_typecheck, iref);
  395.         if ( iesp >= estop )
  396.             return_with_error (e_execstackoverflow, iref);
  397.         store_state(iesp);
  398.         whichp = (iosp[-2].value.index ? iosp - 1 : iosp);
  399.         iosp -= 3;
  400.         /* Open code "up" for the array case(s) */
  401. ifup:        switch( r_type_xe(whichp) )
  402.            {
  403.         default:
  404.             ref_assign(iesp + 1, whichp);
  405.             iref = iesp + 1;
  406.             icount = 0;
  407.             goto top;
  408.         case exec(t_array):
  409.         case exec(t_mixedarray):
  410.         case exec(t_shortarray): ;
  411.            }
  412.         iref = whichp->value.refs;
  413.         icount = r_size(whichp);
  414.         if ( --icount <= 0 )    /* <= 1 more elements */
  415.            {    if ( icount < 0 ) goto up;
  416.            }
  417.         else
  418.            {    iesp++;
  419.             ref_assign(iesp, whichp);
  420.            }
  421.         goto top;
  422.     case plain_exec(tx_op_le):
  423. x_le:        code = obj_le(iosp - 1, iosp);
  424.         if ( code < 0 )
  425.             return_with_error (code, iref);
  426.         iosp--;
  427.         make_bool(iosp, code);
  428.         next();
  429.     case plain_exec(tx_op_pop):
  430. x_pop:        if ( iosp < op_nargs_check(1) )
  431.             return_with_error (e_stackunderflow, iref);
  432.         iosp--;
  433.         next();
  434.     case plain_exec(tx_op_roll):
  435. x_roll:        if ( (code = zroll(iosp)) < 0 )
  436.             return_with_error (code, iref);
  437.         iosp -= 2;
  438.         next();
  439.     case plain_exec(tx_op_sub):
  440. x_sub:        if ( (code = zop_sub(iosp)) < 0 )
  441.             return_with_error (code, iref);
  442.         iosp--;
  443.         next();
  444.     /* Executable types. */
  445.     case plain_exec(t_null):
  446.         goto bot;
  447.     case plain_exec(t_oparray):
  448.         /* Replace with the definition and go again. */
  449.         pvalue =
  450.           &op_array_table.value.refs[op_index(iref) - op_def_count];
  451. prst:        store_state(iesp);
  452. pr:        if ( iesp >= estop )
  453.             return_with_error (e_execstackoverflow, pvalue);
  454.         ++iesp;
  455.         ref_assign(iesp, pvalue);
  456.         iref = pvalue->value.refs;
  457.         icount = r_size(pvalue) - 1;
  458.         if ( icount <= 0 ) { if ( icount < 0 ) goto up; iesp--; }
  459.         goto top;
  460.     case plain_exec(t_operator):
  461.        {    esp = iesp;        /* save for operator */
  462.         osp = iosp;        /* ditto */
  463.         /* Operator routines take osp as an argument. */
  464.         /* This is just a convenience, since they adjust */
  465.         /* osp themselves to reflect the results. */
  466.         /* Operators that (net) push information on the */
  467.         /* operand stack must check for overflow: */
  468.         /* this normally happens automatically through */
  469.         /* the push macro (in oper.h). */
  470.         /* Operators that do not typecheck their operands */
  471.         /* must check explicitly for stack underflow. */
  472.         /* Note that each case must set iosp = osp: */
  473.         /* this is so we can switch on code without having to */
  474.         /* store it and reload it (for dumb compilers). */
  475.         switch ( code = (*real_opproc(iref))(iosp) )
  476.            {
  477.         case 0:            /* normal case */
  478.             iosp = osp;
  479.             next();
  480.         case o_push_estack:    /* store the state and go to up */
  481.             iosp = osp;
  482.             store_state(iesp);
  483.             iesp = esp;
  484.             goto up;
  485.         case o_pop_estack:    /* just go to up */
  486.             iosp = osp;
  487.             if ( esp == iesp ) goto bot;
  488.             iesp = esp;
  489.             goto up;
  490.         case o_reschedule:
  491.             store_state(iesp);
  492.             goto res;
  493.         case e_typecheck:
  494.             /* This might be an operand stack */
  495.             /* underflow: check the required # of */
  496.             /* operands now. */
  497.             if ( osp < osbot - 1 + op_num_args(iref) )
  498.                 code = e_stackunderflow;
  499.             /* (falls through) */
  500.            }
  501.         iosp = osp;
  502.         return_with_error (code, iref);
  503.        }
  504.     case plain_exec(t_name):
  505.         pvalue = iref->value.pname->pvalue;
  506.         if ( !pv_valid(pvalue) )
  507.            {    ref *pdvalue;
  508.             if ( (pdvalue = dict_find_name(iref)) == 0 )
  509.                 return_with_error (e_undefined, iref);
  510.             pvalue = pdvalue;
  511.            }
  512.         switch ( r_type_xe(pvalue) )
  513.            {
  514.         cases_nox():    /* access errors */
  515.             return_with_error (e_invalidaccess, iref);
  516.         case exec(t_array):
  517.         case exec(t_mixedarray):
  518.         case exec(t_shortarray):
  519.             /* This is an executable procedure, execute it. */
  520.             goto prst;
  521.         case plain_exec(tx_op_add): goto x_add;
  522.         case plain_exec(tx_op_dup): goto x_dup;
  523.         case plain_exec(tx_op_exch): goto x_exch;
  524.         case plain_exec(tx_op_if): goto x_if;
  525.         case plain_exec(tx_op_ifelse): goto x_ifelse;
  526.         case plain_exec(tx_op_le): goto x_le;
  527.         case plain_exec(tx_op_pop): goto x_pop;
  528.         case plain_exec(tx_op_roll): goto x_roll;
  529.         case plain_exec(tx_op_sub): goto x_sub;
  530.         case plain_exec(t_operator):
  531.            {    /* Shortcut for operators. */
  532.             /* See above for the logic. */
  533.             esp = iesp;
  534.             osp = iosp;
  535.             switch ( code = (*real_opproc(pvalue))(iosp) )
  536.                {
  537.             case 0:            /* normal case */
  538.                 iosp = osp;
  539.                 next();
  540.             case o_push_estack:    /* store the state and go to up */
  541.                 iosp = osp;
  542.                 store_state(iesp);
  543.                 iesp = esp;
  544.                 goto up;
  545.             case o_pop_estack:    /* just go to up */
  546.                 iosp = osp;
  547.                 if ( esp == iesp ) goto bot;
  548.                 iesp = esp;
  549.                 goto up;
  550.             case o_reschedule:
  551.                 store_state(iesp);
  552.                 goto res;
  553.             case e_typecheck:
  554.                 if ( osp < osbot - 1 + op_num_args(pvalue) )
  555.                     code = e_stackunderflow;
  556.                }
  557.             iosp = osp;
  558.             return_with_error (code, pvalue);
  559.            }
  560.         cases_lit_1():
  561.         cases_lit_2():
  562.         cases_lit_3():
  563.             /* Just push the value */
  564.             if ( iosp >= ostop )
  565.                 return_with_error (e_stackoverflow, pvalue);
  566.             ++iosp;
  567.             ref_assign(iosp, pvalue);
  568.             next();
  569.         default:        /* handles other literals */
  570.             /* Not a procedure, reinterpret it. */
  571.             store_state(iesp);
  572.             icount = 0;
  573.             iref = pvalue;
  574.             goto top;
  575.            }
  576.     case exec(t_file):
  577.        {    /* Executable file.  Read the next token and interpret it. */
  578.            stream *s;
  579.         code = file_check_read(iref, &s);
  580.         if ( code < 0 ) return_with_error (code, iref);
  581. rt:        if ( iosp >= ostop )    /* check early */
  582.           return_with_error (e_stackoverflow, iref);
  583.         osp = iosp;        /* scan_token uses ostack */
  584.         switch ( code = scan_token(s, 0, (ref *)(iosp + 1)) )
  585.            {
  586.         case 0:            /* read a token */
  587.             /* It's worth checking for literals, which make up */
  588.             /* the majority of input tokens, before storing the */
  589.             /* state on the e-stack.  Note that because of //, */
  590.             /* the token may have *any* type and attributes. */
  591.             switch ( r_type(iosp + 1) )
  592.                {
  593.             case t_name: case t_string:
  594.                 if ( r_has_attr(iosp + 1, a_executable) )
  595.                     break;
  596.             /* Executable arrays aren't executed at the */
  597.             /* top level -- they're treated as literals. */
  598.             case t_array: case t_mixedarray: case t_shortarray:
  599.             case t_integer: case t_real:
  600.                 ++iosp;
  601.                 goto rt;
  602.                }
  603.             store_state(iesp);
  604.             /* Push the file on the e-stack */
  605.             if ( iesp >= estop )
  606.                 return_with_error (e_execstackoverflow, iref);
  607.             ++iesp;
  608.             ref_assign(iesp, iref);
  609.             iref = iosp + 1;
  610.             icount = 0;
  611.             goto top;
  612.         case 1:            /* end of file */
  613.             code = file_close(iref, s);
  614.             if ( code < 0 ) return_with_error (code, iref);
  615.             goto bot;
  616.         default:        /* error */
  617.             return_with_error (code, iref);
  618.            }
  619.        }
  620.     case exec(t_string):
  621.        {    /* Executable string.  Read a token and interpret it. */
  622.         stream ss;
  623.         sread_string(&ss, iref->value.bytes, r_size(iref));
  624.         osp = iosp;        /* scan_token uses ostack */
  625.         switch ( code = scan_token(&ss, 1, &token) )
  626.           {
  627.         case 0:            /* read a token */
  628.             store_state(iesp);
  629.             /* Push the updated string back on the e-stack */
  630.             if ( iesp >= estop )
  631.               return_with_error (e_execstackoverflow, iref);
  632.             ++iesp;
  633.             iesp->tas.type_attrs = iref->tas.type_attrs;
  634.             iesp->value.bytes = ss.cptr + 1;
  635.             r_set_size(iesp, ss.cbuf + ss.bsize - ss.cptr - 1);
  636.             iref = &token;
  637.             icount = 0;
  638.             goto top;
  639.         case 1:            /* end of string */
  640.             goto bot;
  641.         default:        /* error */
  642.             return_with_error (code, iref);
  643.           }
  644.        }
  645.     /* Handle packed arrays here by re-dispatching. */
  646.     /* This also picks up some anomalous cases of non-packed arrays. */
  647.     default:
  648.         switch ( *(ushort *)iref >> packed_type_shift )
  649.            {
  650.         case pt_full_ref:
  651.         case pt_full_ref+1:
  652.             if ( iosp >= ostop )
  653.               return_with_error (e_stackoverflow, iref);
  654.             ++iosp;
  655.             /* We know that refs are properly aligned: */
  656.             /* see packed.h for details. */
  657.             ref_assign(iosp, iref);
  658.             next();
  659.         case pt_executable_operator:
  660.            {    uint index = *(ushort *)iref & packed_int_mask;
  661.             op_index_ref(index, &token);
  662.             store_state_short(iesp);
  663.             icount = 0;
  664.             iref = &token;
  665.            }    goto top;
  666.         case pt_integer:
  667.             if ( iosp >= ostop )
  668.               return_with_error (e_stackoverflow, iref);
  669.             ++iosp;
  670.             make_int(iosp, (*(short *)iref & packed_int_mask) +
  671.                     packed_min_intval);
  672.             next_short();
  673.         case pt_literal_name:
  674.         case pt_literal_name+1:
  675.             if ( iosp >= ostop )
  676.               return_with_error (e_stackoverflow, iref);
  677.             ++iosp;
  678.             name_index_ref((uint)*(ushort *)iref &
  679.                          packed_max_name_index,
  680.                        iosp);
  681.             next_short();
  682.         case pt_executable_name:
  683.         case pt_executable_name+1:
  684.            {    ref nref;
  685.             name_index_ref((uint)*(ushort *)iref &
  686.                      packed_max_name_index,
  687.                        &nref);
  688.             pvalue = nref.value.pname->pvalue;
  689.             if ( !pv_valid(pvalue) )
  690.                {    ref *pdvalue;
  691.                 if ( (pdvalue = dict_find_name(&nref)) == 0 )
  692.                   return_with_error (e_undefined, &nref);
  693.                 pvalue = pdvalue;
  694.                }
  695.             switch ( r_type_xe(pvalue) )
  696.                {
  697.             case exec(t_array):
  698.             case exec(t_mixedarray):
  699.             case exec(t_shortarray):
  700.                 /* This is an executable procedure, */
  701.                 /* execute it. */
  702.                 store_state_short(iesp);
  703.                 goto pr;
  704.             default:        /* handles other literals */
  705.                 /* Not a procedure, reinterpret it. */
  706.                 store_state_short(iesp);
  707.                 icount = 0;
  708.                 iref = pvalue;
  709.                 goto top;
  710.                }
  711.            }
  712.         /* default can't happen here */
  713.            }
  714.        }
  715.     /* Literal type, just push it. */
  716.     if ( iosp >= ostop ) return_with_error (e_stackoverflow, iref);
  717.     ++iosp;
  718.     ref_assign(iosp, iref);
  719. bot:    next();
  720. out:    /* At most 1 more token in the current procedure. */
  721.     /* (We already decremented icount.) */
  722.     if ( !icount )
  723.        {    /* Pop the execution stack for tail recursion. */
  724.         iesp--;
  725.         iref++;
  726.         goto top;
  727.        }
  728. up:    /* See if there is anything left on the execution stack. */
  729.     switch ( r_type_xe(iesp) )
  730.        {
  731.     default:
  732.         iref = iesp--;
  733.         icount = 0;
  734.         goto top;
  735.     case exec(t_array):
  736.     case exec(t_mixedarray):
  737.     case exec(t_shortarray): ;
  738.        }
  739.     iref = iesp->value.refs;        /* next element of array */
  740.     icount = r_size(iesp) - 1;
  741.     if ( icount <= 0 )        /* <= 1 more elements */
  742.        {    iesp--;            /* pop, or tail recursion */
  743.         if ( icount < 0 ) goto up;
  744.        }
  745.     goto top;
  746. res:    /* Some operator has asked for context rescheduling. */
  747.     code = (*gs_interp_reschedule_proc)();
  748.     if ( code < 0 ) return_with_error (code, iref);
  749.     /* Reload state information from memory. */
  750.     iosp = osp;
  751.     iesp = esp;
  752.     goto up;
  753. }
  754.  
  755. /* ------ Initialization procedure ------ */
  756.  
  757. op_def interp_op_defs[] = {
  758.         /* Internal operators */
  759.     {"0%interp_exit", interp_exit, &i_interp_exit},
  760.     op_def_end(0)
  761. };
  762.